package com.zmn.mcc.services.impl.organize;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.organize.McOrganize;
import com.zmn.mcc.model.entity.organize.McOrganizeQuery;
import com.zmn.mcc.persistence.interfaces.organize.OrganizeDao;
import com.zmn.mcc.services.interfaces.organize.OrganizeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 17:25
 */
@Service
public class OrganizeServiceImpl implements OrganizeService {

    @Resource
    private OrganizeDao organizeDao;


    @Override
    public McOrganize findByKey(Integer orgId) {
        return organizeDao.findByKey(orgId);
    }

    @Override
    public List<McOrganize> listPageByQuery(McOrganizeQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : organizeDao.listPageByQuery(query);
    }

    @Override
    public List<McOrganize> listAboveByLevel(Integer level, Integer status) {
        McOrganizeQuery query = new McOrganizeQuery();
        query.setLevel(level);
        query.setStatus(status);
        return organizeDao.listAboveByLevel(query);
    }

    @Override
    public List<McOrganize> listByCompanyId(Integer companyId) {
        return this.listByCompanyId(companyId, null);
    }

    @Override
    public List<McOrganize> listByCompanyId(Integer companyId, Integer status) {
        return this.listByCompanyId(companyId, null, status);
    }

    @Override
    public List<McOrganize> listByCompanyId(Integer companyId, Integer type, Integer status) {
        McOrganizeQuery query = new McOrganizeQuery();
        query.setCompanyId(companyId);
        query.setType(type);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McOrganize> listByOrgIds(List<Integer> orgIds) {
        return this.listByOrgIds(orgIds, null);
    }

    @Override
    public List<McOrganize> listByOrgIds(List<Integer> orgIds, Integer status) {
        McOrganizeQuery query = new McOrganizeQuery();
        query.setOrgIds(orgIds);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McOrganize> listByParentId(Integer parentId) {
        return this.listByParentId(parentId, null);
    }

    @Override
    public List<McOrganize> listByParentId(Integer parentId, Integer status) {
        return this.listByParentId(parentId, null, status);
    }

    @Override
    public List<McOrganize> listByParentId(Integer parentId, Integer type, Integer status) {
        McOrganizeQuery query = new McOrganizeQuery();
        query.setParentId(parentId);
        query.setType(type);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McOrganize> listByQuery(McOrganizeQuery query) {
        return organizeDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(McOrganizeQuery query) {
        return organizeDao.countByQuery(query);
    }


    @Override
    public Integer updateByKey(McOrganize organize) {
        if (organize.getUpdateTime() == null) {
            organize.setUpdateTime(DateUtil.getNow());
        }
        return organizeDao.updateByKey(organize);
    }

    @Override
    public Integer updateStatus(Integer orgId, Integer status, String updater) {
        McOrganize organize = new McOrganize();
        organize.setOrgId(orgId);
        organize.setStatus(status);
        organize.setUpdater(updater);
        return this.updateStatus(organize);
    }

    @Override
    public Integer updateStatus(McOrganize organize) {
        if (organize.getUpdateTime() == null) {
            organize.setUpdateTime(DateUtil.getNow());
        }
        return organizeDao.updateByKey(organize);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> orgIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(orgIds);
        dto.setStatus(status.intValue());
        dto.setUpdater(updater);
        return updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return organizeDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateSort(Integer orgId, Integer sort) {
        McOrganize organize = new McOrganize();
        organize.setOrgId(orgId);
        organize.setSort(sort);
        return this.updateSort(organize);
    }

    @Override
    public Integer updateSort(McOrganize organize) {
        return organizeDao.updateSort(organize);
    }


    @Override
    public Integer insert(McOrganize organize) {
        if (NumberUtil.isNullOrZero(organize.getParentId())) {
            organize.setParentId(GlobalConsts.TOP_ID);
        }
        organize.setSort(100);
        organize.setStatus(GlobalDict.STATUS_ENABLE);
        if (organize.getUpdateTime() == null) {
            organize.setUpdateTime(DateUtil.getNow());
        }
        organize.setCreater(organize.getUpdater());
        organize.setCreateTime(organize.getUpdateTime());
        return organizeDao.insert(organize);
    }


    @Override
    public Integer deleteByKey(Integer orgId) {
        return organizeDao.deleteByKey(orgId);
    }

    @Override
    public List<McOrganize> listValidOrgs() {
        return organizeDao.listValidOrgs();
    }

    @Override
    public void updateBsOrg(McOrganize organize) {
        organizeDao.updateBsOrg(organize);
    }
}
